package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class DraftPeer extends BasePeer{
	public static final String tableName = "Draft";
	public static final String DRAFTID = "draftId";
	public static final String USERID = "userId";
	public static final String TITLE = "title";
	public static final String CONTENT = "content";
	public static final String CREATETIME = "createTime";
	public static final String UPDATETIME = "updateTime";
	public static final String DELETED = "deleted";
	public static final String[] columnNames=new String[]{"draftId","userId","title","content","createTime","updateTime","deleted"};
	public static Draft getDraftByPKs(Object draftId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(DraftPeer.tableName,DraftPeer.columnNames," TRUE  AND `draftId`=? ",new Object[]{draftId,},0, 1,conn);
			Draft draft = new Draft();
		fill(rs,draft);
		return draft;
	}
	public static Draft getDraftReferenceColumnsOnlyByPKs(Object draftId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(DraftPeer.tableName,new String[]{"draftId",}," TRUE AND `draftId`=?",new Object[]{draftId,}, 0,  1,conn);
			Draft draft = new Draft();
		fill(rs,draft);
		return draft;
	}
	public static Draft getDraftByDraftId(long draftId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(DraftPeer.tableName,DraftPeer.columnNames,"`draftId`=?",new Object[]{draftId},0, 1,conn);
			Draft draft = new Draft();
		fill(rs,draft);
		return draft;
	}
	public static void fill(ResultSet rs, Draft draft) throws SQLException {
		if(rs.next()){
			try{draft.setDraftId(rs.getLong("draftId"));}catch(Exception ex){}
			try{draft.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{draft.setTitle(rs.getString("title"));}catch(Exception ex){}
			try{draft.setContent(rs.getString("content"));}catch(Exception ex){}
			try{draft.setCreateTime(rs.getTimestamp("createTime"));}catch(Exception ex){}
			try{draft.setUpdateTime(rs.getTimestamp("updateTime"));}catch(Exception ex){}
			try{draft.setDeleted(rs.getBoolean("deleted"));}catch(Exception ex){}
			draft.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<Draft> draftCollection) throws SQLException {
		while(rs.next()){
			Draft draft=new Draft();
			try{draft.setDraftId(rs.getLong("draftId"));}catch(Exception ex){}
			try{draft.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{draft.setTitle(rs.getString("title"));}catch(Exception ex){}
			try{draft.setContent(rs.getString("content"));}catch(Exception ex){}
			try{draft.setCreateTime(rs.getTimestamp("createTime"));}catch(Exception ex){}
			try{draft.setUpdateTime(rs.getTimestamp("updateTime"));}catch(Exception ex){}
			try{draft.setDeleted(rs.getBoolean("deleted"));}catch(Exception ex){}
			draft.resetModified();
			draftCollection.add(draft);
		}
	}
	public static boolean update(Draft draft, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < DraftPeer.columnNames.length; i++) {
			if (((DraftBase)draft).modified[i]) {
				columns.add(DraftPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `Draft` SET ");
		sql.append("WHERE TRUE AND `draftId` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,draft.get(column));
		}
		pstmt.setObject(columns.size()+1+0,draft.get("draftId"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(Draft draft, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (draft.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `Draft` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (draft.get(column) != null) {
				pstmt.setObject(i + 1 + j, draft.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<Draft> getDraftCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<Draft> collection=new Vector<Draft>();
		fill(rs,collection);
		return collection;
	}
}
